import { useState } from 'react';
import Taro, { useDidShow, useLoad } from '@tarojs/taro';
import { contactStore } from '@/business/store/contact/contact';
import { fetchAddressByLocation } from '@/business/services/common/common';
import { nearbyPublicFriendCounts } from '@/business/services/contact';
import { View } from '@tarojs/components';
import NavigationTabs from './components/NavigationTabs';
import MyFriends from './components/MyFriends';
import PublicFriends from './components/PublicFriends';
import cssModule from './index.module.scss';
import { useUserStore } from '@/business/store/user/login';
/**
 * @description 通讯录
 * @author Shawn
 */

export default function Contact() {

  // 获取公开豹友名单
  const getPublicFriendList = async (cityCode?: string) => {
    await contactStore.getState().getPublicFriendList({ current: 1, size: 10 }, false, false, false, cityCode);
  }

  const { vipInfo, vipRemainDays = 0 } = useUserStore();

  // 获取好友名单
  const getFriendList = async () => {
    await contactStore.getState().getFriendList({ current: 1, size: 10 });
  }

  // 获取已申请好友名单
  const getApplyFriendList = async () => {
    await contactStore.getState().getApplyFriendList();
  }

  // 获取附近豹友数量
  const [nearbyFriendsCount, setNearbyFriendsCount] = useState(0);

  const getNearbyFriendsCount = async (
    latitude?: number,
    longitude?: number,
    radius?: number,
  ) => {
    const res = await nearbyPublicFriendCounts(
      { latitude, longitude, radius }
    );
    setNearbyFriendsCount(res?.data || 0);
  }

  // 获取当前城市
  const [cityCode, setCityCode] = useState('');

  const getLocation = async () => {
    const res = await Taro.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
    });
    const { latitude, longitude } = res;
    if (!latitude || !longitude) {
      getPublicFriendList();
    } else {
      getNearbyFriendsCount(latitude, longitude, 10);
      const location = await fetchAddressByLocation({
        latitude: latitude,
        longitude: longitude
      });
      setCityCode(location.data.citycode);
      getPublicFriendList(
        location.data.citycode
      );
    }

  }

  // 当前选中的 tab 索引
  const [tabIndex, setTabIndex] = useState(1);

  const handleTabChange = (index: number) => {
    setTabIndex(index);
    if (index === 0) {
      getFriendList();
    }
    getApplyFriendList();
  }
  const [updateGroup, setUpdateGroup] = useState(false)
  useLoad((options) => {
    if (vipInfo?.vipLevelId === '002' && vipRemainDays <= 0) {
      Taro.redirectTo({
        url: `/subPackages/equity/index`,
      });
      return
    };
    if (options.tabIndex === '0') {
      setTabIndex(0);
      getFriendList();
    }
    contactStore.getState().getGroupFriendList();
  })
  useDidShow(() => {
    getFriendList();
    contactStore.getState().getGroupFriendList();
    getApplyFriendList();
    // if (Taro.getStorageSync('update-group')) {
    //   getFriendList();
    //   contactStore.getState().getGroupFriendList();
    //   getApplyFriendList();
    //   Taro.setStorageSync('update-group', false)
    // }
  })

  return (
    <View>
      {/* 自定义导航栏 */}
      <NavigationTabs tabs={['我的好友 ', '公开豹友']} tabIndex={tabIndex} handleTabChange={handleTabChange} />
      {/* 页面主体内容 */}
      <View className={cssModule.container}>
        {tabIndex === 0 ?
          <MyFriends updateGroup={updateGroup} /> : <PublicFriends cityCode={cityCode} nearbyFriendsCount={nearbyFriendsCount} />
        }
      </View>
    </View>
  );
}
